package net.xdclass.strategy;

import net.xdclass.enums.BizCodeEnum;
import net.xdclass.exception.BizException;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class CustomDBPreciseShardingAlgorithm  implements PreciseShardingAlgorithm<String> {

	@Override
	public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
		String codePrefix = "ds"+preciseShardingValue.getValue().substring(0,1);
		for( String  colPrefix : collection){
			if(codePrefix.equals(colPrefix)){
				return colPrefix;
			}
		}
		throw new BizException(BizCodeEnum.DB_ROUTE_NOT_FOUND);
	}
}
